package utils

import "sync"

type rwMutex struct {
	mutex *sync.RWMutex
}

func rwCreate(safe ...bool) rwMutex {
	if len(safe) > 0 && safe[0] {
		return rwMutex{
			mutex: new(sync.RWMutex),
		}
	}
	return rwMutex{}
}

func (mu *rwMutex) IsSafe() bool {
	return mu.mutex != nil
}

func (mu *rwMutex) Lock() {
	if mu.mutex != nil {
		mu.mutex.Lock()
	}
}

func (mu *rwMutex) Unlock() {
	if mu.mutex != nil {
		mu.mutex.Unlock()
	}
}

func (mu *rwMutex) RLock() {
	if mu.mutex != nil {
		mu.mutex.RLock()
	}
}

func (mu *rwMutex) RUnlock() {
	if mu.mutex != nil {
		mu.mutex.RUnlock()
	}
}
